package rbm.modelado.datos.elementos;

import rbm.modelado.datos.elementos.interfaces.Block;
import rbm.modelado.datos.elementos.interfaces.BlockShape;
import rbm.modelado.datos.elementos.interfaces.Shape;
import rbm.modelado.datos.elementos.interfaces.BoardShape;
import rbm.modelado.global.interfaces.PuzzleConstants;
import rbm.partida.GameState;
import rbm.utiles.*;
import modelado.datos.elementos.ElementoAbstracto;

//Elemento colocado en el tablero

public class BlockImpl extends ElementoAbstracto implements PuzzleConstants,Block
{

   private Integer xPos;
   private Integer yPos;
   private static int xDim;
   private static int yDim;
   private static int zDim;
   
   private static Integer nivel;  //Nivel en el que se encuentra el bloque
 
   private GameState gs;

   private BlockShape shape;

   
	// Arbitrary shape block.
	// Block is an arbitrary dimensioned block, each unit cube
	// of which is filled or not.
	//
	// shape[][][] = x, y, z array of shape
	//
	// Starts with x-axis left-right, y-axis up-down screen.


   public BlockImpl( BlockShape s, Integer x, Integer y, GameState g )
   {
      shape = s;
      xPos = x;
      yPos = y;

      gs = g;

      doDims();
   }


   private void doDims()
   {
      xDim = shape.getXDim();
      yDim = shape.getYDim();
      zDim = shape.getZDim();
   }


   public Boolean roll( Integer dir )
   {
	  
      TesterResults tr = RollTester.testRoll( gs.getFloor(nivel), shape, xPos, yPos, dir );

      if( tr.isLegal() )
      {
         shape = tr.getShape();
         xPos = tr.getXPos();
         yPos = tr.getYPos();

         return true;
      }

      else
         return false;
   }


   public Shape getLayer( Integer l )
   {
	   Integer xDim = shape.getXDim();
	   Integer yDim = shape.getYDim();

	   BoardShape layer = new BoardShapeImpl(xDim,yDim);

      
      for( Integer x = 0; x < xDim; x++ )
         for( Integer y = 0; y < yDim; y++ )
            layer.setContentXY(x, y,  shape.getContentXYZ(x, y, l));

      return layer;
   }


   public Integer getXPos()
   {
      return xPos;
   }

   public Integer getYPos()
   {
      return yPos;
   }


   public Integer getHeight()
   {
      return zDim;
   }


   public static BoardShape getFootprint( BlockShapeImpl shape )
   {
	   BoardShape footprint = new BoardShapeImpl(xDim,yDim);

      for( int i = 0; i < xDim; i++ )
         for( int j = 0; j < yDim; j++ )
            footprint.setContentXY(i, j,  shape.getContentXYZ(i, j, 0));

      return footprint;
   }

	public BlockShape getShape() {
		return shape;
	}



	public Integer getXDim() {
		return xDim;
	}



	public Integer getYDim() {
		return yDim;
	}


	public Integer getZDim() {
		return zDim;
	}
	
	public Object getContentXYZ(Integer i,Integer j,Integer k){
		return shape.getContentXYZ(i, j, k);
	}

	public void setContentXYZ(Integer i, Integer j, Integer k, Object b) {
		shape.setContentXYZ(i, j, k, b);		
	}
	
	public boolean equals(Object e) {
		boolean r = false;
		if (e instanceof BlockImpl) {
			BlockImpl e1 = (BlockImpl) e;
			r = super.getCodigo().equals(e1.getCodigo());
		}
		return r;
	}
}